package com.briup.smartcity_group1.web.controller.login;

import com.briup.smartcity_group1.bean.basic.BaseUser;
import com.briup.smartcity_group1.consts.DataMessageConsts;
import com.briup.smartcity_group1.consts.LoginApiConsts;
import com.briup.smartcity_group1.service.ILoginService;
import com.briup.smartcity_group1.utils.result.Result;
import com.briup.smartcity_group1.web.vo.BaseUserRegisterVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**登录模块controller
 * @author congee(congee02 @ 163.com)
 * @date 1/4/2023 5:10 PM
 */
@RestController
@RequestMapping("")
@CrossOrigin
public class LoginController {

    @Autowired
    private ILoginService service;

    @PostMapping(value = LoginApiConsts.LOGIN, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void login(@RequestParam(required = true) String username,
                      @RequestParam(required = true) String password) {
        String token = service.login(username, password);
        Result.setDataMessage(token);
    }

    @PostMapping(value = LoginApiConsts.REGISTER, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void register(BaseUserRegisterVO vo) {
        BaseUser toRegister = new BaseUser();
        BeanUtils.copyProperties(vo, toRegister);
        Result.setDataMessage(DataMessageConsts.USER_REGISTER_SUCCESS);
        service.register(toRegister);
    }

    @PostMapping(value = LoginApiConsts.LOGOUT)
    public void logout() {
        Result.setDataMessage(DataMessageConsts.USER_QUIT_SUCCESS);
        service.logout();
    }

}
